package com.bizfx.component.views
{	
	import com.bizfx.collections.HashMap;
	import com.bizfx.component.fields.IField;
	import com.bizfx.events.HashMapChangedEvent;
	import com.bizfx.events.ModuleCommandEvent;
	import com.bizfx.events.ViewCommandEvent;
	import com.bizfx.events.ViewEvent;
	import com.bizfx.utils.ListUtils;
	
	import mx.collections.ArrayCollection;
	import mx.collections.ArrayList;
	import mx.collections.IList;
	import mx.containers.Box;
	import mx.events.CollectionEvent;
	import mx.events.FlexEvent;
	import mx.logging.ILogger;
	import mx.logging.Log;

	[Event(name="viewLoaded", type="com.bizfx.events.ViewEvent")]
	
	[Event(name="viewShowed", type="com.bizfx.events.ViewEvent")]
	
	[Event(name="viewHided", type="com.bizfx.events.ViewEvent")]
	
	[Event(name="viewCommandRaise", type="com.bizfx.events.ViewCommandEvent")]
	
	[Event(name="viewDataSourceChanged", type="flash.events.Event")]
		
	public class BaseView extends Box implements IView
	{
		protected var logger:ILogger;
		
		private var commandsMap:HashMap = new HashMap();
		
		protected var commandsDirty:Boolean = false;
		
		public function BaseView()
		{
			logger = Log.getLogger(this.className);
			super();
			this.addEventListener(FlexEvent.CREATION_COMPLETE, view_CreationCompleteHandler);
		}
		
		private var _viewKey:String;
		/**
		 * 视图标识
		 */
		[Bindable]
		public function get viewKey():String {
			return _viewKey;
		}
		
		public function set viewKey(value:String):void {
			_viewKey = value;
		}
		
		private var _viewName:String;
		/**
		 * 视图名称
		 */
		[Bindable]
		public function get viewName():String {
			return _viewName;
		}
		
		public function set viewName(value:String):void {
			_viewName = value;
		}
		
		protected var _viewDataSource:Object;
		
		[Bindable("viewDataSourceChanged")]
		public function get viewDataSource():Object {
			if (_viewDataSource == null) {
				_viewDataSource = new ObjectProxy(new Object());
			}
			this.retrieveViewDataFromForm();
			return _viewDataSource;
		}
		
		public function set viewDataSource(value:Object):void {
			if (_viewDataSource != value) {
				_viewDataSource = value;
				this.dispatchEvent(new Event("viewDataSourceChanged"));	
			}
			this.bindViewDataToForm();
		}
		
		private var _status:String;
		[Bindable]
		public function get status():String
		{
			return _status;
		}
		
		public function set status(value:String):void
		{
			_status = value;
		}
		
		private var _module:IModule;
		
		public function get module():IModule
		{
			return _module;
		}
		
		public function set module(value:IModule):void
		{
			_module = value;
			if (this._module) {
				this._module.addEventListener(ModuleCommandEvent.ModuleCommand_Handle, moduleCommandHandleHandler);
			}
		}
		
		private var _commands:IList;
		[Bindable("viewCommandsChanged")]
		public function get commands():IList {
			if (this._commands == null) {
				this._commands = new ArrayCollection();
				this._commands.addEventListener(CollectionEvent.COLLECTION_CHANGE, commandsChangeHandler);
			}
			return _commands;
		}
		
		public function set commands(value:IList):void {
			if (this._commands != value) {
				this.commandsDirty = true;
				if (_commands) {
					this._commands.removeEventListener(CollectionEvent.COLLECTION_CHANGE, commandsChangeHandler);
				}
				this._commands = value;
				if (_commands) {
					_commands.addEventListener(CollectionEvent.COLLECTION_CHANGE, commandsChangeHandler);
				}
				this.invalidateProperties();
				this.invalidateDisplayList();
//				this.dispatchEvent(new ViewEvent(ViewEvent.View_Commands_Changed));
			}
		}
		
		/**
		 * 通过键值获取字段
		 */
		public function getViewCommandByKey(commandKey:String):ViewCommand {
			for each (var command:ViewCommand in this.commands) {
				if (command.commandKey == commandKey) {
					return command;
				}
			}
			return null;
		}
		
		public function show():void {
			this.visible = true;
			this.includeInLayout = true;
			var e:ViewEvent = new ViewEvent(ViewEvent.View_Showed);
			this.dispatchEvent(e);
		}
		
		public function hide():void {
			this.visible = false;
			this.includeInLayout = false;
			var e:ViewEvent = new ViewEvent(ViewEvent.View_Hided);
			this.dispatchEvent(e);
		}
		
		public function refresh():void {
			this.dispatchEvent(new ViewEvent(ViewEvent.View_Refresh));
		}
		
		private function view_CreationCompleteHandler(event:FlexEvent):void {
			logger.debug("处理【" + this.className + "】的CreationComplete事件。");
			var e:ViewEvent = new ViewEvent(ViewEvent.View_Loaded);
			this.dispatchEvent(e);
			this.bindViewDataToForm();
		}
	
		public function bindViewDataToForm():void {
		}
		
		protected function retrieveViewDataFromForm():void {
		}
		
		public function bindInitViewDataToForm():void {
		}
		
//		public function hideAllViewCommands():void {
//		}
//		
//		public function showViewCommand(commandKey:String):void {
//		}
	
		private function commandsChangeHandler(event:CollectionEvent):void {
			this.commandsDirty = true;
		}
		
		private function moduleCommandHandleHandler(event:ModuleCommandEvent):void {
			trace("BaseView.moduleCommandHandleHandler");
			this.dispatchEvent(new ModuleCommandEvent(ModuleCommandEvent.ModuleCommand_Handle, event.commandType));
		}
	}
}